/**
 * Created by shichunguang on 16/10/12.
 */

import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    ListView,
    Image,
    TouchableOpacity,
    Platform,
    AsyncStorage,
} from 'react-native';

var Dimensions = require('Dimensions');
var Width = Dimensions.get('window').width; //屏宽
var Height = Dimensions.get('window').height;//屏高

var MineBottomView = React.createClass({
    // 初始化函数
    getInitialState(){

        var  getSectionData = (dataBlob, sectionID) => {
            return dataBlob[sectionID];
        };

        var  getRowData = (dataBlob, sectionID, rowID) => {
            return dataBlob[sectionID + ':' + rowID];
        };

        return{
            dataSource: new ListView.DataSource({
                getSectionData: getSectionData, // 获取组中数据
                getRowData: getRowData, // 获取行中的数据
                rowHasChanged: (r1, r2) => r1 !== r2,
                sectionHeaderHasChanged:(s1, s2) => s1 !== s2
            })
        }

    },


    componentDidMount(){
        this.loaddata();
    },
    loaddata(){
        var token = '';
        try {
            AsyncStorage.getItem(
                'token',
                (error, result)=> {
                    if (error) {
                        alert('取值失败:' + error);
                    } else {
                        // alert('取值成功:'+result);
                        this.token = result
                    }
                }
            )
        } catch (error) {
            alert('失败' + error);
        }
        var fetchOptions = {
            method: 'POST',
            headers: {
                'token': this.token
            },

        };
        let url = 'http://www.pintasty.cn/user/meerkatinfo';
        fetch(url, fetchOptions)
            .then((response) => response.json())
            .then((responseJSON) => {
                if (responseJSON.status == 200) {
                    //success(responseJSON)//访问成功
                    // console.log('responseJson == ' + responseJSON);
                        //拿数据
                    var jsonData = responseJSON.data.categoryStatisticsInfo;

                    // 定义一些变量
                    var dataBlob = {},
                        sectionIDs = [],
                        rowIDs = [],
                        wines = [];

                    // 遍历
                    for(var i=0; i<jsonData.length; i++){
                        // 1. 把组号放入sectionIDs数组中
                        sectionIDs.push(i);

                        // 2.把组中内容放入dataBlob对象中
                        dataBlob[i] = jsonData[i].categoryName

                        // 3. 取出该组中所有的车
                        wines = jsonData[i].productStatisticsInfo;
                        rowIDs[i] = [];

                        // 4. 遍历所有的车数组
                        for(var j=0; j<wines.length; j++){
                            // 把行号放入rowIDs
                            rowIDs[i].push(j);
                            // 把每一行中的内容放入dataBlob对象中
                            dataBlob[i+':'+j] = wines[j];
                        }
                    }
                    this.setState({
                        dataSource: this.state.dataSource.cloneWithRowsAndSections(dataBlob,sectionIDs,rowIDs)
                    })
                } else if (responseJSON.status == 301) {
                    // alert('请登录')

                    
                } else {
                    alert('访问服务器失败')

                }
            }).done();
    },
    render(){
        return(
            <View style={{backgroundColor:'white',width:Width}}>
                <View>
                    <Image source={{uri:'guoqi'}} style={{width:Width-180,height:Width-180,borderRadius:(Width-180)/2,marginLeft:90,marginTop:10}} />
                </View>
                <ListView
                    bounces={false}
                    dataSource={this.state.dataSource}
                    renderRow={this.renderRow}
                    renderSectionHeader={this.renderSectionHeader}

                />
            </View>
        );
    },
    // 每一行的数据
    renderRow(rowData){
        return(
                <View style={{borderBottomWidth:0.5,borderBottomColor:'#e8e8e8',height:30}}>
                    <Text style={{marginLeft:10,fontSize:18,marginTop:6}}>{rowData.productName}</Text>
                </View>
        );
    },

    // 每一组中的数据
    renderSectionHeader(sectionData, sectionID){
        return(
            <View style={{height:20,borderBottomWidth:0.5,borderBottomColor:'#e8e8e8',marginTop:10}}>
                <Text style={{marginLeft:10,marginTop:3,fontSize:14}}>{sectionData}</Text>
            </View>
        );
    }

});


var styles = StyleSheet.create({

});

module.exports = MineBottomView;